<?php

namespace app\wechat\controller;

use app\api\controller\Wechats;
use think\Controller;
use think\Db;

class Registers extends Controller
{
    public function index()
    {
        if ($this->request->isGet()) {
            $uid = (int) $this->request->get('uid'); // 代理/机构ID
            $type = $this->request->get('type'); // type: 1机构，2代理
            $data = [];
            if ($uid > 0 && $type > 0) {
                // 机构
                if ($type == 1) {

                } else {

                }
                return json(["code" => 20000, "msg" => "请求成功", "data" => $data]);
            } else {
                return json(["code" => 50000, "msg" => "参数错误"]);
            }
        } else if ($this->request->isPost()) {
            return json(["code" => 20000, "msg" => "注册成功"]);
        }
        return json(["code" => 50000, "msg" => "请求方式错误"]);
    }

    // 微信扫码登录及绑定
    public function backstage()
    {
        if ($this->request->isGet()) {
            $type = $this->request->get('type');
            $only_no = $this->request->get('only_no');
            $code = $this->request->get('code');
            $wx_open_id = "";
            $html_title = "扫码登录";
            if ($type == 2) {
                $html_title = "账号绑定";
            }
            $returns = ["code" => 200, "msg" => "", "notice" => ""];
            if (!empty($code)) {
                $returns = self::getWxInfo($code, $type, $only_no);
                if ($type == 1) {
                    Db::name("scanning_record")->insert(["only_no" => $only_no, "create_time" => date("Y-m-d H:i:s")]);
                    $returns["notice"] = "是否允许账号《" . $returns["data"]["username"] . "》登录?";
                } else {
                    $returns["notice"] = "是否要绑定《" . $returns["data"]["username"] . "》该账号?";
                }
                $wx_open_id = $returns["data"]["wx_open_id"];
            } else {
                $url = config("app_url") . "/wechat/Registers/backstage?type=" . $type . "&only_no=" . $only_no;
                $urls = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . config("wechat")["AppID"] . "&redirect_uri=" . urlencode($url) . "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
                header("location:" . $urls);
            }
            $this->view->assign('data', $returns);
            $this->view->assign('type', $type);
            $this->view->assign('only_no', $only_no);
            $this->view->assign('wx_open_id', $wx_open_id);
            $this->view->assign('html_title', $html_title);
            return $this->view->fetch();
        }
    }

    /**
     * 获取微信信息
     * code         微信换取的code
     * type         类型：1扫码登录，2绑定微信
     * only_no      二维码唯一标识
     */
    public static function getWxInfo($code, $type, $only_no)
    {
        $username = "";
        $msg = "";
        $codes = 200;
        if (!empty($type)) {
            if (!empty($code)) {
                $wx_user = Wechats::getWxInfo($code);
                if (!empty($wx_user)) {
                    $count = Db::name("admin")->where('wx_open_id', $wx_user)->where('status', 'normal')->count();
                    if ($type == 1) {
                        $username = Db::name("admin")->where('wx_open_id', $wx_user)->where('status', 'normal')->value("username");
                        // 扫码登录
                        if ($count <= 0) {
                            $codes = 500;
                            $msg = "抱歉，您的微信未绑定平台，无法登陆！";
                        }
                    } else {
                        $username = Db::name("admin")->where('id', $only_no)->where('status', 'normal')->value("username");
                        // 绑定二维码
                        if ($count > 0) {
                            $codes = 500;
                            $msg = "该微信已绑定其他账号，请使用其他微信扫码！";
                        }
                    }
                } else {
                    $msg = "操作失败，请重新扫码！";
                    $codes = 500;
                }
                return ["code" => $codes, "msg" => $msg, "data" => ['wx_open_id' => $wx_user, "username" => $username]];
            }
        }
    }

    public function test()
    {
        $data = [
            "name" => "王佳",
            "bank_card_number" => "6214834711553165",
            "idcards" => "150223199003181214",
            "phone" => "18748149147"
        ];
        $return = bankCardVerifi($data);
        dump($return);
    }
}